package cn.net.autocode.core.utils;

import jakarta.servlet.http.HttpServletRequest;

public class ClientIpUtils {

    public static String getIpAddr(HttpServletRequest request) {
        String ip = null;
        try {
            ip = request.getHeader("x-forwarded-for")==null?"":request.getHeader("x-forwarded-for").trim();
            if (ip.isBlank() || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP")==null?"":request.getHeader("Proxy-Client-IP").trim();
            }
            if (ip.isBlank() || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP")==null?"":request.getHeader("WL-Proxy-Client-IP").trim();
            }
            if (ip.isBlank() || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP")==null?"":request.getHeader("HTTP_CLIENT_IP").trim();
            }
            if (ip.isBlank() || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR")==null?"":request.getHeader("HTTP_X_FORWARDED_FOR").trim();
            }
            if (ip.isBlank() || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ip;
    }


}
